<template>
  <div class="login">
    <h1>登录</h1>
    <div class="login-wrapper">
      <div class="avatar">
        <img src="@/assets/images/R-C.jpg" alt="">  
      </div>
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="username"
            name="username"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="password"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
        </van-cell-group>
        <div style="margin: 16px">
            <!-- 关联submit -->
          <van-button round block type="primary" native-type="submit">
            登录
          </van-button>
        </div>
      </van-form>
    </div>
    <p class="register" @click="router.push('/register')">新用户？点击注册</p>
    <!-- 使用vanti的输入框 -->
  </div>
</template>

<script setup>
import axios from '@/api/index';

import {ref} from 'vue';
import checkUsernameAndPassword  from '@/utils/index.js';
import { showToast } from 'vant';
import { useRouter } from 'vue-router';

const username = ref('');
const password = ref('');
const router = useRouter();

//提交函数
const onSubmit = async (values) => {
    // console.log(values);
    // 向后端发起请求 将账号密码传递过去

        const res = await axios.post('/user/login',values);
        localStorage.setItem('userInfo' , JSON.stringify(res.data));
        localStorage.setItem('token' , res.token);
        router.push('/noteClass');
}
</script>

<style lang="less" scoped>
.login{
    width: 100vw;
    height: 100vh;
    background-color: #fff;
    padding: 0 0.3rem;
    /* 默认是标准盒模型 */
    box-sizing: border-box;
    //设置为BFC
    overflow-x: auto;
    position: relative;

    h1{
        height: 0.6933rem;
        font-size: 0.48rem;
        text-align: center;
        line-height: 0.6933rem;
        /*margin 重叠 BFC容器 */
        margin-top: 1.12rem;
    }

    .login-wrapper{
        width: 7.44rem;
        border: 1px solid rgb(199, 197, 189);
        margin: 0 auto;
        margin-top: 1.7rem;
        border-radius: 0.3rem;
        box-shadow: 0 0.15rem 0.533rem 10px rgb(214, 213, 213);
        padding-bottom: 15px;
        .avatar{
            width: 2.4rem;
            height: 2.4rem;
            margin: 1rem auto 0.77rem auto;
            border-radius: 50%;
            overflow: hidden;
            img{
                /*不设高度防止变形 */
                width: 100%;
            }
        }
    }

    .register{
        position: absolute;
        bottom: 30px;
        left: 50%;
        transform: translate(-50%);  
        color: rgb(56, 160, 245)
    }
}

/*修改UI框架内容 深度修改*/
:deep(.van-field__label){
    width: 45px;
}
</style>
